﻿package controllers.myinfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.User;

import models.Demandinfo;
import utils.DateUtil;
import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class DemandinfoController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public View getDemandinfoItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		User user = (User) rbac.getCurrentUser();
		String orderBy = "editdate desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (address like '%"+q+"%' or context like '%"+q+"%' ) ";			
		}
		
		if (!isAdmin())
            cond += " and z_id = '" + user.getDepartment().getCompany_id() +"'";

		Object[] args = tmpArgs.toArray();
		
		long total = Demandinfo.count(Demandinfo.class, cond, args);
		List<Demandinfo> items = Demandinfo.findAll(Demandinfo.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View saveDemandinfo(Demandinfo item, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		User user = (User) rbac.getCurrentUser();
		item.setOper(user.getLogin());
		item.setValid_time(DateUtil.getValidDay2(item.getValid_time_hist()));
		item.setInputdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setZ_id(user.getDepartment().getCompany_id());
		item.setUnitname(user.getDepartment().getSxname());
		if (item.save()){
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")新建需求信息，内容为：" + new JsonView(item));									

			result.put("success", true);
		} else {
			result.put("failure", true);
			result.put("msg", getErrorMsg(item));
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View updateDemandinfo(int id, Map<String,Object> params, String[] rids) throws Exception {
		Map<String,Object> result = new HashMap<String,Object>();
		Demandinfo item = Demandinfo.find(Demandinfo.class, id);
		JsonView olditem= new JsonView(item);
		item = Demandinfo.updateModel(item, params);
    User user = (User) rbac.getCurrentUser();
    item.setValid_time(DateUtil.getValidDay2(item.getValid_time_hist()));
		item.setOper(user.getLogin());
		item.setEditdate(new java.sql.Timestamp(System
				.currentTimeMillis()));
		item.setZ_id(user.getDepartment().getCompany_id());
		item.setUnitname(user.getDepartment().getSxname());
		if ((isAdmin()) || (item.getOper().equals(user.getLogin())))
		{
			if (item.save()){
				Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改需求信息，原内容为："+ olditem + "\r\n修改内容为：" + new JsonView(item));										

				result.put("success", true);
			} else {
				result.put("failure", true);
				result.put("msg", getErrorMsg(item));
			}
		}else{
			result.put("failure", true);
			result.put("msg", "你没有权限操作这条记录。");			
		}
		
		View view = new JsonView(result);
		view.setContentType("text/plain;charset=utf-8");
		return view;
	}
	
	public View destroyDemandinfo(int id) throws Exception {
		Demandinfo item = Demandinfo.find(Demandinfo.class, id);
		User user = (User) rbac.getCurrentUser();
		if ((isAdmin()) || (item.getOper().equals(user.getLogin())))
		{
			Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除需求信息，内容为：" + new JsonView(item));									
			
    		item.destroy();
		}
		return new JsonView("success:true");
	}
	
}
